{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 基础",
   "id": "a0f542e29f165ef6"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:11.601605Z",
     "start_time": "2025-01-22T13:17:11.468126Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 导包\n",
    "from bs4 import BeautifulSoup\n",
    "import requests"
   ],
   "id": "c4fbcd26f5593323",
   "outputs": [],
   "execution_count": 2
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:12.737961Z",
     "start_time": "2025-01-22T13:17:12.292690Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 如下的三行实际上是获取一个满足html格式的字符串（res.text）\n",
    "url = 'http://python123.io/ws/demo.html'\n",
    "res = requests.get(url)\n",
    "res_text = res.text  # 服务器返回响应\n",
    "res_text"
   ],
   "id": "70d67f33bb861003",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'<html><head><title>This is a python demo page</title></head>\\r\\n<body>\\r\\n<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\\r\\n<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\\r\\n<a href=\"http://www.icourse163.org/course/BIT-268001\" class=\"py1\" id=\"link1\">Basic Python</a> and <a href=\"http://www.icourse163.org/course/BIT-1001870001\" class=\"py2\" id=\"link2\">Advanced Python</a>.</p>\\r\\n</body></html>'"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 3
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:12.756615Z",
     "start_time": "2025-01-22T13:17:12.748260Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 构造BeautifulSoup对象，需要传入待解析的字符串，指定的解析器\n",
    "soup = BeautifulSoup(res_text, \"lxml\") # 使用lxml解析器\n",
    "soup"
   ],
   "id": "fa169a11ea162d76",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<html><head><title>This is a python demo page</title></head>\n",
       "<body>\n",
       "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>\n",
       "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>\n",
       "</body></html>"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 4
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:13.124390Z",
     "start_time": "2025-01-22T13:17:13.118024Z"
    }
   },
   "cell_type": "code",
   "source": "type(soup)",
   "id": "3e3d548058ae89e2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bs4.BeautifulSoup"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 5
  },
  {
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:14.226422Z",
     "start_time": "2025-01-22T13:17:14.221752Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 使用prettify()格式化显示输出\n",
    "print(soup.prettify())"
   ],
   "id": "initial_id",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<html>\n",
      " <head>\n",
      "  <title>\n",
      "   This is a python demo page\n",
      "  </title>\n",
      " </head>\n",
      " <body>\n",
      "  <p class=\"title\">\n",
      "   <b>\n",
      "    The demo python introduces several python courses.\n",
      "   </b>\n",
      "  </p>\n",
      "  <p class=\"course\">\n",
      "   Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
      "   <a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">\n",
      "    Basic Python\n",
      "   </a>\n",
      "   and\n",
      "   <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">\n",
      "    Advanced Python\n",
      "   </a>\n",
      "   .\n",
      "  </p>\n",
      " </body>\n",
      "</html>\n",
      "\n"
     ]
    }
   ],
   "execution_count": 6
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:15.571130Z",
     "start_time": "2025-01-22T13:17:15.565504Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取html的title标签\n",
    "soup.title"
   ],
   "id": "d6dcbc09bedeca36",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<title>This is a python demo page</title>"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 7
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:16.135827Z",
     "start_time": "2025-01-22T13:17:16.128007Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取html的a标签的信息(soup.a默认获取第一个a标签)\n",
    "soup.a"
   ],
   "id": "a1ea3bbfc6d96f78",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 8
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:16.714537Z",
     "start_time": "2025-01-22T13:17:16.709902Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取html的a标签的信息的另一种写法\n",
    "soup.find('a')"
   ],
   "id": "5ce9780695518b65",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 9
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:17.328864Z",
     "start_time": "2025-01-22T13:17:17.323377Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取所有的a标签，可以使用find_all\n",
    "soup.find_all('a')"
   ],
   "id": "65ce57849d6663d9",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>,\n",
       " <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 10
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:17.969638Z",
     "start_time": "2025-01-22T13:17:17.963360Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 实际上通过soup.a、soup.title或者soup.find返回的都是Tag对象，它们有一些公共的属性\n",
    "# <a>xxx</a>\n",
    "type(soup.title), type(soup.a), type(soup.find('a')), type(soup.find_all('a')[0])"
   ],
   "id": "f6eda31d96426086",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(bs4.element.Tag, bs4.element.Tag, bs4.element.Tag, bs4.element.Tag)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 11
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:18.631603Z",
     "start_time": "2025-01-22T13:17:18.626789Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取a标签的名字\n",
    "# soup.a.name\n",
    "tag = soup.a\n",
    "tag.name"
   ],
   "id": "18c2c6d12c4729c2",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'a'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 12
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:19.274971Z",
     "start_time": "2025-01-22T13:17:19.269199Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取a标签包含的属性\n",
    "soup.a.attrs"
   ],
   "id": "8ac6a9c9bafac656",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'href': 'http://www.icourse163.org/course/BIT-268001',\n",
       " 'class': ['py1'],\n",
       " 'id': 'link1'}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 13
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T13:17:20.048997Z",
     "start_time": "2025-01-22T13:17:20.042779Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 获取a标签中元素的值\n",
    "soup.a.text"
   ],
   "id": "82df18c0eef6f27e",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Basic Python'"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 14
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:59:03.592707Z",
     "start_time": "2025-01-22T09:59:03.586604Z"
    }
   },
   "cell_type": "code",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p class=\"course\">Python is a wonderful general-purpose programming language. You can learn Python from novice to professional by tracking the following courses:\n",
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a> and <a class=\"py2\" href=\"http://www.icourse163.org/course/BIT-1001870001\" id=\"link2\">Advanced Python</a>.</p>"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 25,
   "source": [
    "# a标签的父标签(上一级标签)\n",
    "soup.a.parent"
   ],
   "id": "61aa566d72ff48ba"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:46:49.210333Z",
     "start_time": "2025-01-22T09:46:49.205289Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# a标签的父标签的父标签的名字\n",
    "soup.a.parent.parent.name"
   ],
   "id": "89150681642ef3d3",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'body'"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 17
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "#### 进阶：通过属性、内容等方式查找",
   "id": "cb77a6ba0a151760"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:53:12.831266Z",
     "start_time": "2025-01-22T09:53:12.820281Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查找满足特定属性的tag\n",
    "# 查找class属性等于'title'的tag\n",
    "soup.find(attrs={'class': 'title'})"
   ],
   "id": "bda777aff6daca76",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<p class=\"title\"><b>The demo python introduces several python courses.</b></p>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 18
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:54:28.445386Z",
     "start_time": "2025-01-22T09:54:28.440365Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查找id属性等于link1的tag\n",
    "soup.find(attrs={'id': 'link1'})"
   ],
   "id": "d3a2e27bbdaf8b98",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<a class=\"py1\" href=\"http://www.icourse163.org/course/BIT-268001\" id=\"link1\">Basic Python</a>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 19
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-01-22T09:55:22.447707Z",
     "start_time": "2025-01-22T09:55:22.442716Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 查找指定内容的tag\n",
    "soup.find(string='Basic Python')"
   ],
   "id": "5c8d94faf24f5c2c",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Basic Python'"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 20
  },
  {
   "metadata": {},
   "cell_type": "code",
   "outputs": [],
   "execution_count": null,
   "source": "",
   "id": "9eaae134682147fa"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
